package com.groupbuying.ordosmarathon.usergame;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/ordosmarathon")
public class UserAndGameController {

    @Autowired
    private UserAndGameDAO userAndGameDAO;

    @GetMapping("/login")
    public String login(String userId, int gameId) {

        String returnResult = "";

        UserAndGameDO userAndGameDO = userAndGameDAO.get(userId, gameId);

        if (userAndGameDO == null) {
            if (gameId == 1){
                return  "3";
            }
            return "1"; /* 首次登陆 */
        } else {
            long deadline = userAndGameDO.getDeadline().getTime();

            /* 非首次，时间有效 */
            if (System.currentTimeMillis() - deadline <= 0) {
                returnResult = "2" + "-" + (deadline - System.currentTimeMillis());
            }

            /* 非首次，时间过期 */
            if (System.currentTimeMillis() - deadline > 0) {
                returnResult = "3";
            }
        }
        return returnResult;
    }


    /* 设置登陆信息 */
    @GetMapping("/setInfo")
    public void setLoginInfo(String userId, int gameId) {
        UserAndGameDO userAndGameDO = new UserAndGameDO();
        userAndGameDO.setUserId(userId);
        userAndGameDO.setGameId(gameId);
        long time = System.currentTimeMillis();
        /* 设置deadline */
        if (userAndGameDO.getGameId() == 1) {
            //+30分钟
            userAndGameDO.setDeadline(new Date(time + 30 * 60 * 1000L));
        } else if (userAndGameDO.getGameId() == 2) {
            //+30分钟
            userAndGameDO.setDeadline(new Date(time + 30 * 60 * 1000L));
        } else {
            //+30天
            userAndGameDO.setDeadline(new Date(time + 30 * 24 * 60 * 60 * 1000L));
        }
        userAndGameDAO.save(userAndGameDO);
    }

}
